package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.Voucher;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/23.
 */

public class VoucherListAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<Voucher> dataSet;
    private View.OnClickListener onClickListener;

    public VoucherListAdapter(Context context, List<Voucher> datas) {
        mContext = context;
        dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_voucher_item, null);
        view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;
        Voucher item = dataSet.get(position);
            if (item.getStatus() == 1) {
                //已使用
                listHolder.layout1.setBackgroundResource(R.mipmap.voucher_left_used);
                listHolder.imgFlag.setVisibility(View.VISIBLE);
                listHolder.imgFlag.setImageResource(R.mipmap.voucher_used);
            } else if (item.getStatus() == 2) {
                //已过期
                listHolder.layout1.setBackgroundResource(R.mipmap.voucher_left_expired);
                listHolder.imgFlag.setVisibility(View.VISIBLE);
                listHolder.imgFlag.setImageResource(R.mipmap.voucher_expired);
            } else {
                listHolder.layout1.setBackgroundResource(R.mipmap.voucher_left_unused);
                listHolder.imgFlag.setVisibility(View.GONE);
            }
        listHolder.voucherDesc.setText(item.getTitle());
        listHolder.voucherMoney.setText(String.format("¥%s", item.getMoney().setScale(2, RoundingMode.HALF_UP)));
        listHolder.voucherFrom.setText("来源:"+item.getFrom());
        listHolder.voucherTime.setText(String.format("有效期至%s",item.getEndDate()));
        listHolder.totalLayout.setOnClickListener(onClickListener);
        listHolder.totalLayout.setTag(item);


    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.voucher_money)
        TextView voucherMoney;
        @Bind(R.id.layout1)
        RelativeLayout layout1;
        @Bind(R.id.voucher_desc)
        TextView voucherDesc;
        @Bind(R.id.voucher_from)
        TextView voucherFrom;
        @Bind(R.id.voucher_time)
        TextView voucherTime;
        @Bind(R.id.layout2)
        LinearLayout layout2;
        @Bind(R.id.img_flag)
        ImageView imgFlag;
        @Bind(R.id.total_layout)
        LinearLayout totalLayout;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
